﻿using Microsoft.VisualStudio.TestTools.UnitTesting;
using PivotData;
using PivotIO.XML;
using System.Configuration;
using System.IO;

namespace PivotIO.Tests
{
    [TestClass]
    public class FacetCategoryIOTest
    {
        [TestInitialize]
        public void TestReset()
        {
            PivotIO.Tests.Utils.TestInit();
        }

        [TestCleanup]
        public void TestFinalize()
        {
            Utils.TestFinalize();
        }

        [TestMethod]
        public void SimpleCategoryIO()
        {
            var cs = new FacetCategory[] {
                new FacetCategory() { IsFilterVisible = true, IsMetaDataVisible = false, IsWordWheelVisible = true, NumberFormat = "hi" },
                new FacetCategory() { IsFilterVisible = false, IsMetaDataVisible = true, IsWordWheelVisible = false, NumberFormat = "no"}
            };

            FacetCategoryIO.SaveAllFacets(1, cs);
            FacetCategoryIO.WaitTillIOFlushed();
            FacetCategoryIO.ResetCache();
            var cs1 = FacetCategoryIO.LoadAllFacets(1);

            Assert.AreEqual(2, cs1.Length, "# facets");
            Assert.IsTrue(cs1[0].IsFilterVisible, "filter for #1");
            Assert.AreEqual("no", cs1[1].NumberFormat, "Number format #2");
        }

        [TestMethod]
        public void RenderingPending()
        {
            var cs = new FacetCategory[] {
                new FacetCategory() { IsFilterVisible = true, IsMetaDataVisible = false, IsWordWheelVisible = true, NumberFormat = "hi" },
                new FacetCategory() { IsFilterVisible = false, IsMetaDataVisible = true, IsWordWheelVisible = false, NumberFormat = "no"}
            };

            FacetCategoryIO.SaveAllFacets(1, cs);
            FacetCategoryIO.WaitTillIOFlushed();
            Assert.IsTrue(Utils.RenderPending(1), "render pending");
        }

        [TestMethod]
        public void RenderNotPendingAfterNullUpdate()
        {
            var cs = new FacetCategory[] {
                new FacetCategory() { IsFilterVisible = true, IsMetaDataVisible = false, IsWordWheelVisible = true, NumberFormat = "hi" },
                new FacetCategory() { IsFilterVisible = false, IsMetaDataVisible = true, IsWordWheelVisible = false, NumberFormat = "no"}
            };

            FacetCategoryIO.SaveAllFacets(1, cs);
            FacetCategoryIO.WaitTillIOFlushed();
            var dir = new DirectoryInfo(Path.Combine(ConfigurationManager.AppSettings["ROOTXMLDirectory"], "RenderUpdatesPending"));
            dir.Delete(true);
            FacetCategoryIO.SaveAllFacets(1, cs);
            FacetCategoryIO.WaitTillIOFlushed();
            Assert.IsFalse(Utils.RenderPending(1), "Render should not be pending after null update");
        }
    }
}
